#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString imagePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("图片文件 (*.png *.jpg *.bmp)"));
    if (!imagePath.isEmpty()) {
        QPixmap pixmap(imagePath);
        ui->label_7->setPixmap(pixmap.scaled(ui->label_7->size(), Qt::KeepAspectRatio));
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    // 清空所有数据
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->lineEdit_4->clear();
    ui->spinBox->setValue(0);
    ui->radioButton->setChecked(false);
    ui->radioButton_2->setChecked(false);
    ui->comboBox->setCurrentIndex(0);
    ui->label_7->clear();
}

void MainWindow::on_pushButton_3_clicked()
{
    // 表单验证
    QString errorMessage;
    if (ui->lineEdit->text().isEmpty()) {
        errorMessage += "姓名不能为空\n";
    }
    if (ui->lineEdit_2->text().isEmpty()) {
        errorMessage += "学号不能为空\n";
    }
    if (ui->comboBox->currentText().isEmpty()) {
        errorMessage += "请选择学院\n";
    }
    if (ui->lineEdit_4->text().isEmpty()) {
        errorMessage += "专业不能为空\n";
    }

    if (ui->spinBox->value() == 0) {
        errorMessage += "年龄不能为空\n";
    }
    if (!ui->radioButton->isChecked() && !ui->radioButton_2->isChecked()) {
        errorMessage += "请选择性别\n";
    }

    if (!errorMessage.isEmpty()) {
        QMessageBox messageBox;
        messageBox.setWindowTitle(tr("表单验证失败"));
        messageBox.setIcon(QMessageBox::Warning);
        messageBox.setText(errorMessage);
        messageBox.exec();
        return;
    }
    QString message = "姓名：" + ui->lineEdit->text() + "\n";
    message += "学号：" + ui->lineEdit_2->text() + "\n";
    message += "性别：";
    if (ui->radioButton->isChecked()) {
        message += "男\n";
    } else if (ui->radioButton_2->isChecked()) {
        message += "女\n";
    } else {
        message += "未知\n";
    }
    message += "年龄：" + QString::number(ui->spinBox->value()) + "\n";
    message += "学院：" + ui->comboBox->currentText() + "\n";
    message += "专业：" + ui->lineEdit_4->text() + "\n";

    QMessageBox::information(this,tr("学生信息"),message);

}

